package com.sdhs.costcontrol.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sdhs.costcontrol.domain.GcControlChangeManageList;
import com.sdhs.costcontrol.service.IGcControlChangeManageListService;
import com.sdhs.listmanage.domain.ManageTreeSelect;
import com.sdhs.listmanage.domain.entity.GcManageList;
import com.sdhs.listmanage.service.IGcManageListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.costcontrol.domain.GcControlChangeManagement;
import com.sdhs.costcontrol.service.IGcControlChangeManagementService;
import com.sdhs.common.utils.poi.ExcelUtil;
import com.sdhs.common.core.page.TableDataInfo;

/**
 * 变更管理Controller
 * 
 * @author wanglei
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/costcontrol/changeManage")
public class GcControlChangeManagementController extends BaseController
{
    @Autowired
    private IGcControlChangeManagementService gcControlChangeManagementService;

    @Autowired
    private IGcManageListService gcManageListService;
    @Autowired
    private IGcControlChangeManageListService gcControlChangeManageListService;
    /**
     * 查询变更管理列表
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:changeManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(GcControlChangeManagement gcControlChangeManagement)
    {
        startPage();
        List<GcControlChangeManagement> list = gcControlChangeManagementService.selectGcControlChangeManagementList(gcControlChangeManagement);
        return getDataTable(list);
    }

    /**
     * 查询清单管理树列表
     */
    @PreAuthorize("@ss.hasPermi('manageList:list')")
    @GetMapping("/treeList")
    public AjaxResult treeList(GcControlChangeManagement gcControlChangeManagement) {
        AjaxResult ajax = AjaxResult.success();
        GcManageList gcManageList = new GcManageList();
        gcManageList.setProcurementType("0");
        gcManageList.setProjectId(gcControlChangeManagement.getProjectId());
        List<GcManageList> gcManageLists = gcManageListService.selectGcManageListList(gcManageList);
        List<GcControlChangeManageList> gcControlChangeManageLists = new ArrayList<>();
        if(gcControlChangeManagement.getId()!=null){
             gcControlChangeManageLists = gcControlChangeManageListService.selectGcControlChangeManageByIdList(gcControlChangeManagement.getId());
        }

        // 根据id组装成map树形结构
        Map<Long, GcManageList> manageListMap = gcManageLists.stream()
                .collect(Collectors.toMap(GcManageList::getId, Function.identity()));

        if (CollectionUtil.isNotEmpty(gcControlChangeManageLists)) {
            for (GcControlChangeManageList changeManageItem : gcControlChangeManageLists) {
                GcManageList manageList = BeanUtil.toBean(changeManageItem, GcManageList.class);
                manageList.setId(changeManageItem.getManageId());

                // 若ID已存在则替换，不存在则新增
                if (manageListMap.containsKey(manageList.getId())) {
                    manageListMap.put(manageList.getId(), manageList);
                } else {
                    manageListMap.put(manageList.getId(), manageList);
                    gcManageLists.add(manageList);
                }
            }
        }

        List<ManageTreeSelect> manageTreeSelects = gcManageListService.buildDeptTreeSelect(new ArrayList<>(manageListMap.values()));
        ajax.put("manageTreeList", manageTreeSelects);
        return ajax;
    }
    /**
     * 导出变更管理列表
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:changeManage:export')")
    @Log(title = "变更管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GcControlChangeManagement gcControlChangeManagement)
    {
        List<GcControlChangeManagement> list = gcControlChangeManagementService.selectGcControlChangeManagementList(gcControlChangeManagement);
        ExcelUtil<GcControlChangeManagement> util = new ExcelUtil<GcControlChangeManagement>(GcControlChangeManagement.class);
        util.exportExcel(response, list, "变更管理数据");
    }

    /**
     * 获取变更管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:changeManage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gcControlChangeManagementService.selectGcControlChangeManagementById(id));
    }

    /**
     * 新增变更管理
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:changeManage:add')")
    @Log(title = "变更管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GcControlChangeManagement gcControlChangeManagement)
    {
        return success(gcControlChangeManagementService.insertGcControlChangeManagement(gcControlChangeManagement));
    }

    /**
     * 修改变更管理
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:changeManage:edit')")
    @Log(title = "变更管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GcControlChangeManagement gcControlChangeManagement)
    {
        return toAjax(gcControlChangeManagementService.updateGcControlChangeManagement(gcControlChangeManagement));
    }

    /**
     * 删除变更管理
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:changeManage:remove')")
    @Log(title = "变更管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gcControlChangeManagementService.deleteGcControlChangeManagementByIds(ids));
    }
}
